//
//  JWAnimationManager.m
//  JWBrowser
//
//  Created by Dejun Liu on 15/6/11.
//  Copyright (c) 2015年 JW. All rights reserved.
//

#import "JWAnimationManager.h"
#import "JHChainableAnimations.h"

#define PopViewDefautTag 10000
#define PopAnimationDuration 0.36f
#define PopMaskOpacity 0.5f

@interface JWAnimationManager()

/**
 *  程序的主窗口
 */
@property (nonatomic,strong) UIWindow *keyWindow;

/**
 *  外层容器，放到window上面，然后添加蒙版和
 */
@property (nonatomic,strong) UIView *outContainner;

/**
 *  内层灰色蒙版
 */
@property (nonatomic,strong) UIView *innerMask;

@property (nonatomic,copy) BlockResponse tapMaskBlock;
@property (nonatomic) JWAnimationLocation startAnimationLocaton;
@property (nonatomic) JWAnimationLocation endAnimationLocaton;

@end

@implementation JWAnimationManager

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.keyWindow = [UIApplication sharedApplication].keyWindow;
        
        self.outContainner = [[UIView alloc] initWithFrame:self.keyWindow.bounds];
        self.innerMask = [[UIView alloc] initWithFrame:self.outContainner.bounds];
        [self.outContainner addSubview:self.innerMask];
        
        self.innerMask.backgroundColor = [UIColor blackColor];
        self.innerMask.layer.opacity = PopMaskOpacity;
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMask)];
        [self.innerMask addGestureRecognizer:ges];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationDidChangeStatusBarOrientationNotification:) name:@"UIApplicationDidChangeStatusBarOrientationNotification" object:nil];
    }
    return self;
}

- (void)applicationDidChangeStatusBarOrientationNotification:(NSNotification *)notification
{
    self.innerMask.width = self.keyWindow.width;
    self.innerMask.height = self.keyWindow.height;
    UIView *popView = [self.keyWindow viewWithTag:PopViewDefautTag];
    switch (self.endAnimationLocaton) {
        case JWAnimationLocation_Top:
        {
            popView.top = self.keyWindow.top;
        }
            break;
        case JWAnimationLocation_Right:
        {
            popView.right = self.keyWindow.right;
        }
            break;
        case JWAnimationLocation_Bottom:
        {
            popView.bottom = self.keyWindow.bottom;
        }
            break;
        case JWAnimationLocation_Left:
        {
            popView.left = self.keyWindow.left;
        }
            break;
        case JWAnimationLocation_Center:
        {
            popView.center = self.keyWindow.center;
        }
            break;
            
        default:
            break;
    }
    if (self.isPopViewAdaptWindowWidth) {
        popView.width = self.keyWindow.width;
    }
    if (self.isPopViewAdaptWindowHeight) {
        popView.height = self.keyWindow.height;
    }
}

- (void)didTapMask
{
    if (self.tapMaskBlock) {
        self.tapMaskBlock();
    }else
    {
        UIView *popView  = [self.keyWindow viewWithTag:PopViewDefautTag];
        [popView removeFromSuperview];
    }
}

- (void)popView:(UIView *)view startLocation:(JWAnimationLocation)startLocation endLocation:(JWAnimationLocation)location tapMaskBlock:(BlockResponse) block
{
    self.startAnimationLocaton = startLocation;
    self.endAnimationLocaton = location;
    view.tag = PopViewDefautTag;
    self.tapMaskBlock = block;
    self.innerMask.frame = self.keyWindow.frame;
    [self.keyWindow addSubview:self.outContainner];
    [self.outContainner addSubview:view];
    
    self.innerMask.layer.opacity = 0.0f;
    
    //屏幕旋转情况适配视图
    if (self.isPopViewAdaptWindowHeight) {
        view.height = self.keyWindow.height;
    }
    
//    if (self.isPopViewAdaptWindowWidth) {
//        view.width = self.keyWindow.width;
//    }
    
    view.hidden = YES;
    view.center = self.keyWindow.center;
    
    //动画
    switch (startLocation) {
        case JWAnimationLocation_Top:
        {
            view.bottom = 0.0f;
        }
            break;
        case JWAnimationLocation_Right:
        {
            view.left = SCREEN_WIDTH;
        }
            break;
        case JWAnimationLocation_Bottom:
        {
            view.top = SCREEN_HEIGHT;
        }
            break;
        case JWAnimationLocation_Left:
        {
            view.right = 0.0f;
        }
            break;
        case JWAnimationLocation_Center:
        {
            view.center = _outContainner.center;
        }
            break;
            
        default:
            break;
    }
    view.hidden = NO;
    self.innerMask.makeOpacity(PopMaskOpacity).easeOutCubic.animate(PopAnimationDuration);
    switch (location) {
        case JWAnimationLocation_Top:
        {
            view.makeY(0.0f).easeOutCubic.animate(PopAnimationDuration);
        }
            break;
        case JWAnimationLocation_Right:
        {
            view.makeX(SCREEN_WIDTH).easeOutCubic.animate(PopAnimationDuration);
        }
            break;
        case JWAnimationLocation_Bottom:
        {
            view.makeY(SCREEN_HEIGHT - view.frame.size.height).easeOutCubic.animate(PopAnimationDuration);
        }
            break;
        case JWAnimationLocation_Left:
        {
            view.makeX(0.0f).easeOutCubic.animate(PopAnimationDuration);
        }
            break;
        case JWAnimationLocation_Center:
        {
            CGPoint center = _outContainner.center;
            view.makeCenter(center.x,center.y - 50).easeOutCubic.animate(PopAnimationDuration);
        }
            break;
            
        default:
            break;
    }
}

- (void)removeViewFromPopView:(UIView *)view toLocation:(JWAnimationLocation) endLocation complationBlock:(BlockResponse) complation
{

    switch (endLocation) {
        case JWAnimationLocation_Top:
        {
            view.makeY(0.0f).animate(PopAnimationDuration);
        }
            break;
        case JWAnimationLocation_Right:
        {
            view.makeX(SCREEN_WIDTH).animate(PopAnimationDuration);
        }
            break;
        case JWAnimationLocation_Bottom:
        {
            view.makeY(SCREEN_HEIGHT ).animate(PopAnimationDuration);
        }
            break;
        case JWAnimationLocation_Left:
        {
            view.makeX(0.0f).animate(PopAnimationDuration);
        }
            break;
        case JWAnimationLocation_Center:
        {
            view.makeCenter(_outContainner.center.x,_outContainner.center.y).animate(PopAnimationDuration);
        }
            break;
            
        default:
            break;
    }
    self.innerMask.makeOpacity(0.0f).animate(PopAnimationDuration);
    
    __weak UIView *weakView = view;
    __weak JWAnimationManager *weakSelf = self;
    view.animationCompletion = JHAnimationCompletion(){
        [weakView removeFromSuperview];
        [weakSelf.outContainner removeFromSuperview];
        if (complation) {
            complation();
        }
    };
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.keyWindow = nil;
    DLog(@"JWAnimationManager释放！");
}


@end
